package isg3.data;

import isg3.domain.Concurso;
import isg3.domain.Usuario;
import isg3.utils.UIDGenerator;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class JDBCUsuariosPorConcursoDAO implements IUsuariosPorConcursoDAO {


	private Connection conn;
	
	public JDBCUsuariosPorConcursoDAO(){
		conn = ConnectionManager.getInstance().checkOut();
		System.out.println("SE HA CREADO LA CONEXION");
	}
	public List selectAllUsuarios(String OIDConcurso) {
		
		PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM usuarios"; //WHERE (tipo= tipoU)";
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            //stmt.setString(4, tipo);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
            	Usuario temp = new Usuario();
                temp.setNombre(result.getString("nombre"));
                temp.setApellidos(result.getString("apellidos"));
                temp.setUsuario(result.getString("user"));
	            temp.setPassword(result.getString("password"));
	            temp.setTipo(result.getString("tipo"));
	            temp.setIdUsuario(result.getString("OIDUsuario"));
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
	}
	
	public void insert(String idC, String idU){
		String oido = UIDGenerator.getInstance().getKey();
        PreparedStatement stmt = null;
        String sql = "INSERT INTO usuariosporconcurso ( OIDUpc, concursoOID, usuarioOID) VALUES (?, ?, ?) ";
        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, oido);
            stmt.setString(2, idC);
            stmt.setString(3, idU);
    

            stmt.executeUpdate();


        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
        	System.out.println("SE HA CERRADO LA CONEXION");
        	try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
		
	}
	
	public List selectConcursos(String idU){
		PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM   concursos INNER JOIN usuariosporconcurso ON concursos.OIDConcurso = usuariosporconcurso.concursoOID WHERE (usuariosporconcurso.usuarioOID = ?)";
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            stmt.setString(1, idU);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
            	Concurso temp = new Concurso();
                temp.setTitulo(result.getString("titulo"));
                temp.setDescripcion(result.getString("descripcion"));
                temp.setEstado(result.getString("estado"));
                temp.setIdConcurso(result.getString("OIDConcurso"));
                temp.setFechaConcurso(result.getDate("fechaCon"));
                temp.setFechaInscripcion(result.getDate("fechaIns"));
                
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
        
        	
	}
	
	public Concurso selectConcursosUsuario(String oidc,String oidu){
		PreparedStatement stmt = null;
        ResultSet result = null;
        Concurso c=null;
        
        try {

            String sql = "SELECT * FROM   usuariosporconcurso WHERE (usuarioOID = ? AND concursoOID = ?)";
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            stmt.setString(1, oidu);
            stmt.setString(2, oidc);
            stmt.executeQuery();
            result = stmt.executeQuery();

            result.next();
            c = new Concurso();
            c.setIdConcurso(result.getString("concursoOID"));
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally { 
            ConnectionManager.getInstance().checkIn(conn);
            System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return c;
        
        	
	}
	
	public List selectAllUsuarios(String tipo, String oide){
		PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM  usuarios INNER JOIN usuariosporconcurso ON usuarios.OIDUsuario = usuariosporconcurso.usuarioOID WHERE (usuarios.tipo = ? AND usuariosporconcurso.concursoOID = ?)";
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            stmt.setString(1, tipo);
            stmt.setString(2, oide);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
            	Usuario temp = new Usuario();
                temp.setNombre(result.getString("nombre"));
                temp.setApellidos(result.getString("apellidos"));
                temp.setIdUsuario(result.getString("OIDUsuario"));
                temp.setUsuario(result.getString("user"));
                
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
        
	}

}
